/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.delivery.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.DeliveryTemplateType;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.delivery.common.model.Transport;
import com.yami.shop.delivery.common.service.TransportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2018/11/16.
 */
@RestController
@RequestMapping("/platform/transport")
@Api(tags = "平台端运费模板接口")
public class TransportController {

    @Autowired
    private TransportService transportService;

    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('platform:transport:page')")
    @ApiOperation(value = "分页查询运费模板信息")
    public ServerResponseEntity<IPage<Transport>> page(Transport transport, PageParam<Transport> page) {
        IPage<Transport> transports = transportService.page(page,
                new LambdaQueryWrapper<Transport>()
                        .eq(Transport::getShopId, 0L)
                        .like(StringUtils.isNotBlank(transport.getTransName()), Transport::getTransName, transport.getTransName())
                        .orderByDesc(Transport::getCreateTime)
        );
        return ServerResponseEntity.success(transports);
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "通过id查询运费模板")
    @ApiImplicitParam(name = "id", value = "运费模板id", required = true, dataType = "Long")
    public ServerResponseEntity<Transport> info(@PathVariable("id") Long id) {
        Transport transport = transportService.getTransportAndAllItems(id);
        return ServerResponseEntity.success(transport);
    }

    @PostMapping
    @PreAuthorize("@pms.hasPermission('platform:transport:save')")
    @ApiOperation(value = "新增运费模板")
    public ServerResponseEntity<Void> save(@RequestBody Transport transport) {
        if (DeliveryTemplateType.isSysTemplate(transport.getTransName())) {
            // 与系统内置模板名称重复，请重新输入
            throw new YamiShopBindException("yami.delivery.name.is.same");
        }
        transport.setShopId(0L);
        Date createTime = new Date();
        transport.setCreateTime(createTime);
        transportService.insertTransportAndTransfee(transport);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @PreAuthorize("@pms.hasPermission('platform:transport:update')")
    @ApiOperation(value = "修改运费模板")
    public ServerResponseEntity<Void> update(@RequestBody Transport transport) {
        if (DeliveryTemplateType.isSysTemplate(transport.getTransName())) {
            // 与系统内置模板名称重复，请重新输入
            throw new YamiShopBindException("yami.delivery.name.is.same");
        }
        transportService.updateTransportAndTransfee(transport);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('platform:transport:delete')")
    @ApiOperation(value = "根据运费模板id列表批量删除运费模板")
    @ApiImplicitParam(name = "ids", value = "运费模板id", required = true, dataType = "Long")
    public ServerResponseEntity<Void> delete(@RequestBody Long[] ids) {
        transportService.deleteTransportAndTransfeeAndTranscity(ids);
        // 删除运费模板的缓存
        for (Long id : ids) {
            transportService.removeTransportAndAllItemsCache(id);
        }
        return ServerResponseEntity.success();
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取运费模板列表")
    public ServerResponseEntity<List<Transport>> list() {
        List<Transport> list = transportService.list(new LambdaQueryWrapper<Transport>()
                .eq(Transport::getShopId, 0L)
                .orderByDesc(Transport::getCreateTime)
        );
        return ServerResponseEntity.success(list);
    }
}
